package com.android.semita;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class LocationMapActivity extends Activity {

	public static final String INVOICE_NO="InvoiceNo";
	public static final String TARGETDATE="TargetDate";
	public static final String ADDRESS = "Address";
	public static final String ADDRESSGPS="AddressGPS";
	public static final String INVOICESTATUS="InvoiceStatus";
	public static final String CUSTOMERNAME="CustomerName";
	public static final String LATITUDE ="latitude";
	public static final String LONGTITUDE ="longtitude";
	
	private GoogleMap myMap;
	ArrayList<LatLng> myMarkPoints;
	TextView txtView;
	private String customer_name;
	private String address_text;
	private String invoice_no;

	private LatLng location;
	Intent derivedIntent;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location_map);

		//System.out.println("begin map location activity");
		myMarkPoints = new ArrayList<LatLng>();
		txtView = (TextView) findViewById(R.id.txtResult);
		//System.out.println("find view completed");

		myMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();

		// add button current location on map
		myMap.setMyLocationEnabled(true);

		// Get Intent and retreive extra data from intent from display on screen
		derivedIntent = getIntent();
		location = new LatLng(derivedIntent.getDoubleExtra(LATITUDE, 0.0d),derivedIntent.getDoubleExtra(LONGTITUDE,0.0d));
 		customer_name = derivedIntent.getStringExtra(CUSTOMERNAME);
 		address_text = derivedIntent.getStringExtra(ADDRESS);
 		invoice_no = derivedIntent.getStringExtra(INVOICE_NO);

		// Mock LatLng Nida = new LatLng(13.768856, 100.652519);
		myMap.addMarker(new MarkerOptions()
				.title(address_text)
				.snippet(address_text)
				.position(location));
		// zoom map
		myMap.setMyLocationEnabled(true);
		myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));

		txtView.setText("Invoice No: " +  invoice_no);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.location_map, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// set the way to display 5 ways
		// myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		// myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		// myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		// myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
		// myMap.setMapType(GoogleMap.MAP_TYPE_NONE);
		switch (item.getItemId()) {
		case R.id.action_settings:
			myMarkPoints.clear();
			myMap.clear();
			// txtView.setText("");
			break;
		case R.id.MAP_TYPE_SATELLITE:
			myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
			break;
		case R.id.MAP_TYPE_HYBRID:
			myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
			break;
		case R.id.MAP_TYPE_NORMAL:
			myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
